namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class UserPositionBLL
    {
        public int Add(UserPosition model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserPosition(");
            builder.Append("PositionName");
            builder.Append(") values (");
            builder.Append("@PositionName");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionName", model.PositionName) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int PositionID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserPosition ");
            builder.Append(" where PositionID=@PositionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = PositionID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string PositionIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserPosition ");
            builder.Append(" where ID in (" + PositionIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<UserPosition> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM UserPosition ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            UserPosition item = null;
            List<UserPosition> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<UserPosition>();
                foreach (DataRow row in table.Rows)
                {
                    item = new UserPosition();
                    if (row["PositionID"].ToString() != "")
                    {
                        item.PositionID = int.Parse(row["PositionID"].ToString());
                    }
                    item.PositionName = row["PositionName"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public UserPosition GetModel(int PositionID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select PositionID, PositionName  ");
            builder.Append("  from UserPosition ");
            builder.Append(" where PositionID=@PositionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = PositionID;
            UserPosition position = new UserPosition();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["PositionID"].ToString() != "")
                {
                    position.PositionID = int.Parse(row["PositionID"].ToString());
                }
                position.PositionName = row["PositionName"].ToString();
                return position;
            }
            return null;
        }

        public bool Update(UserPosition model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update UserPosition set ");
            builder.Append(" PositionName = @PositionName  ");
            builder.Append(" where PositionID=@PositionID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionID", model.PositionID), new SqlParameter("@PositionName", model.PositionName) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

